﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace SlamDunk.CMS.Common
{
    /// <summary>
    /// 常用工具类
    /// </summary>
    public static class CommonAssistant
    {
        #region 01.字符串转换MD5扩展方法
        /// <summary>
        /// 字符串转换MD5扩展方法
        /// </summary>
        /// <param name="str">待转换的字符串</param>
        /// <returns>MD5值</returns>
        public static string ToMD5(this string str)
        {
            byte[] buffer = Encoding.Default.GetBytes(str);
            using (MD5 md5 = MD5.Create())
            {
                buffer = md5.ComputeHash(buffer);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < buffer.Length; i++)
                {
                    sb.Append(buffer[i].ToString("x2"));
                }
                return sb.ToString();
            }
        }
        #endregion

        #region 02.忽略大小写比较字符串是否相等扩展方法
        /// <summary>
        /// 忽略大小写比较字符串是否相等扩展方法
        /// </summary>
        /// <param name="source">当前字符串</param>
        /// <param name="target">要比较的字符串</param>
        /// <returns>是否相同</returns>
        public static bool IsSame(this string source, string target)
        {
            return string.Equals(source, target, StringComparison.CurrentCultureIgnoreCase);
        }
        #endregion

        #region 03.判断控制器或方法上是否贴有某特性标签
        /// <summary>
        /// 判断控制器或方法上是否贴有某特性标签
        /// </summary>
        /// <typeparam name="T">特性标签类型</typeparam>
        /// <param name="action">ActionDescriptor</param>
        /// <returns>是否拥有该特性</returns>
        public static bool HasAttribute<T>(ActionDescriptor action)
        {
            Type type = typeof(T);
            return action.IsDefined(type, false) || action.ControllerDescriptor.IsDefined(type, false);
        }
        #endregion

        #region 04.object序列化Json字符串扩展方法
        /// <summary>
        /// object序列化Json字符串扩展方法
        /// </summary>
        /// <param name="obj">object及其子类对象</param>
        /// <returns>Json字符串</returns>
        public static string ToJson(this object obj)
        {
            System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
            return jss.Serialize(obj);
        }
        #endregion

        #region 05.字符串是否为空或Null扩展方法
        /// <summary>
        /// 字符串是否为空或Null扩展方法
        /// </summary>
        /// <param name="str">要判断的字符串</param>
        /// <returns>是否为空或Null</returns>
        public static bool IsNullOrEmpty(this string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 06.字符串过滤Html标签扩展方法
        /// <summary>
        /// 字符串过滤Html标签扩展方法
        /// </summary>
        /// <param name="str">待过虑的字符串</param>
        /// <returns>过滤后的字符串</returns>
        public static string NoHtml(this string str)
        {
            if (str.IsNullOrEmpty())
            {
                return string.Empty;
            }
            str = Regex.Replace(str, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"<style[^>]*?>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"</style>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"<p[^>]*?>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"<div[^>]*?>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"</p>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"</div>", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"-->", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"<!--.*", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "<[^>]*>", "", RegexOptions.Compiled);
            str = Regex.Replace(str, @"([\r\n])[\s]+", " ", RegexOptions.Compiled);
            return str.Replace("&nbsp;", " ");
        }
        #endregion

        #region 07.获取流的MD5值扩展方法
        /// <summary>
        /// 获取流的MD5值
        /// </summary>
        /// <param name="stream">流</param>
        /// <returns>MD5值</returns>
        public static string ToMD5(this Stream stream)
        {
            using (MD5 md5 = MD5.Create())
            {
                byte[] buffer = md5.ComputeHash(stream);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < buffer.Length; i++)
                {
                    sb.Append(buffer[i].ToString("x2"));
                }
                return sb.ToString();
            }
        }
        #endregion

        #region 08.字符串过滤SQL语句扩展方法
        /// <summary>
        /// 字符串过滤SQL语句扩展方法
        /// </summary>
        /// <param name="str">SQL字符串</param>
        /// <returns>过滤后的SQL字符串</returns>
        public static string FilterSql(this string str)
        {
            return str.Replace("'", string.Empty);
        }
        #endregion

        #region 09.省份集合静态属性
        /// <summary>
        /// 省份集合静态属性
        /// </summary>
        public static List<string> ProvList
        {
            get
            {
                return new List<string>() { "北京", "天津", "上海", "重庆", "河北", "山西", "内蒙古", "辽宁", "吉林", "黑龙江", "江苏", "浙江", "安徽", "福建", "江西", "山东", "河南", "湖北", "湖南", "广东", "广西", "海南", "四川", "贵州", "云南", "西藏", "陕西", "甘肃", "青海", "宁夏", "新疆", "香港", "澳门", "台湾" };
            }
        }
        #endregion

        #region 10.相对路径转换绝对路径方法
        /// <summary>
        /// 相对路径转换绝对路径方法
        /// </summary>
        /// <param name="relativePath">相对路径</param>
        /// <returns>绝对路径</returns>
        public static string MapPath(string relativePath)
        {
            relativePath = relativePath.Replace("/", @"\").Substring(1);
            return string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, relativePath);
        }
        #endregion
    }
}
